/**
 * 新建用户
 */
var addOrUpdUsersAddress = function(id, title, userId) {
	var addOrUpdUserAddressForm = new Ext.form.FormPanel({
		id : 'addOrUpdUserAddressForm',
		frame : true,
		buttonAlign : 'center',
		labelAlign : 'right',
		labelWidth : 75,
		layout : 'form',
		fileUpload : true,
		monitorValid : true,
		bodyBorder : false,
		buttons : [{
					text : "保存",
					width : 80,
					id : 'userSave',
					formBind : true,
					handler : function() {
						insertOrUpdUserAddress();
					}
				}, {
					text : "取消",
					width : 80,
					handler : function() {
						addOrUpdUserAddressWin.close();
					}
				}],
		items : [{

			layout : 'column',
			border : false,
			items : [{
				// 第一列
				columnWidth : .47,
				layout : 'form',
				border : false,
				items : [{
							xtype : "textfield",
							id : 'userAddress_consignee',
							fieldLabel : "<span style='color:#F00'>*</span>收货人姓名",
							anchor : '100%',
							name : "consignee"
						}]
			}, {
				// 第二列
				columnWidth : .48,
				layout : 'form',
				border : false,
				items : [{
							xtype : "numberfield",
							id : 'userAddress_consignPhone',
							fieldLabel : "<span style='color:#F00'>*</span>收货人电话",
							anchor : '100%',
							name : "consignPhone"
						}]
			}]
		}, {
			xtype : "panel",
			baseCls : "x-plain",
			layout : "column",
			border : false,
			items : [{
				labelWidth : 60,
				baseCls : "x-plain",
				layout : 'form',
				border : false,
				style : {
					marginLeft : '16'
				},
				items : [{
					layout : "column",
					border : false,
					fieldLabel : "<span style='color:#F00'>*</span>所在地区",
					isFormField : true,
					items : [{
						layout : 'column',
						border : false,
						items : [{
									xtype : "hidden",
									name : "id",
									value : id
								}, {
									xtype : "hidden",
									name : "userId",
									value : userId
								}, {
									xtype : 'comboprovince',
									width : 80,
									id : 'userAddress_province',
									allowBlank : false,
									blankText : "省份不能为空",
									hiddenName : 'provinceName',
									listeners : {
										'select' : function(obj) {
											Ext.getCmp('userAddress_City').reset();
										}
									}
								}, {
									style : {
										marginLeft : '3px',
										marginTop : '5px',
										marginRight : '2px'
									},
									xtype : 'displayfield',
									value : '省'
								},{
									xtype : 'combocity',
									width : 80,
									allowBlank : false,
									blankText : "城市不能为空",
									id : 'userAddress_City',
									hiddenName : 'cityName',
									parentProvinceId : 'userAddress_province',
									listeners : {
										'select' : function(obj) {
											Ext.getCmp('userAddress_region').reset();
											var userAddress_City =Ext.getCmp("userAddress_City");
											userAddress_City.setValue(obj.lastSelectionText);
										}
									}
								}, {
									style : {
										marginLeft : '3px',
										marginTop : '5px',
										marginRight : '2px'
									},
									xtype : 'displayfield',
									value : '市'
								}, {
									xtype : 'comboregion',
									width : 110,
									allowBlank : false,
									blankText : "区（县）不能为空",
									id : 'userAddress_region',
									hiddenName : 'districtName',
									parentCityId : 'userAddress_City',
									listeners : {
										'select' : function(obj) {
										}
									}
								}, {
									style : {
										marginLeft : '3px',
										marginTop : '5px',
										marginRight : '2px'
									},
									xtype : 'displayfield',
									value : '区（县）'
								}]
					}]
				}]
			}]
		}, {
			xtype : "textfield",
			fieldLabel : "<span style='color:#F00'>*</span>街道地址",
			anchor : '95%',
			name : "address"
		}],
		reader : new Ext.data.JsonReader({
					success : 'success',
					root : 'data'
				}, [{
							name : "id"
						}, {
							name : "userId"
						}, {
							name : "consignee"
						}, {
							name : "consignPhone"
						}, {
							name : "provinceName"
						}, {
							name : "cityName"
						}, {
							name : "districtName"
						}, {
							name : "address"
						}, {
							name : "status"
						}, {
							name : "isDefault"
						}, {
							name : "createdDate"
						}, {
							name : "createdName"
						}, {
							name : "updatedDate"
						}, {
							name : "updatedName"
						}])

	});

	var addOrUpdUserAddressWin = new Ext.Window({
				id : 'addOrUpdUserAddressWin',
				width : 470,
				title : title,
				height : 180,
				layout : "form",
				modal : true,// 遮罩效果，弹出此window，背景为灰色不可操作
				plain : true,
				labelWidth : 60,
				draggable: false, // 不可拖动
				resizable:false,  // 不可调整
				bodyStyle : "padding-top: 0px; padding-left:0px;",
				border : false,
				items : [addOrUpdUserAddressForm],
				listeners : {
					'show' : function() {
						selectUserAddressById(id);
					},
					'beforeshow' : function() {
						addOrUpdUserAddressWin.center();
					}
				}
			});
	return addOrUpdUserAddressWin;
}

/**
 * 添加或修改用户地址
 */
function insertOrUpdUserAddress() {
	var userForm = Ext.getCmp("addOrUpdUserAddressForm");
	if (userForm.getForm().isValid()) {
		Ext.Msg.confirm("提示框", "确认要保存地址信息吗?", function(button) {
					if (button == "yes") {
						Ext.getCmp("userAddress_province").setValue(Ext.getCmp("userAddress_province").getRawValue());
						Ext.getCmp("userAddress_City").setValue(Ext.getCmp("userAddress_City").getRawValue());
						Ext.getCmp("userAddress_region").setValue(Ext.getCmp("userAddress_region").getRawValue());
						userForm.getForm().submit({
							url : __ctxPath
									+ "/addressManager/addOrUpdUserAddress",
							method : "post",
							waitTitle : "请稍候",
							waitMsg : "正在提交表单，请稍候......",
							success : function(response, options) {
								var jsonData = Ext.util.JSON
										.decode(options.response.responseText);
								Ext.MessageBox.show({
											title : "信息提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
								Ext.getCmp("addOrUpdUserAddressWin").close();
								var gridPanel = Ext.getCmp('userAddressGrid');
								if (typeof(gridPanel) != 'undefined') {
									gridPanel.store.reload();
								}
							},
							failure : function(response, options) {
								var jsonData = Ext.util.JSON
										.decode(options.response.responseText);
								Ext.MessageBox.show({
											title : "错误提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
							}
						});
					}
				});
	}
}

/**
 * 根据id查找地址对象
 * @param {} id
 */
function selectUserAddressById(id){
	if (id != null && id != "undefined") {
		Ext.getCmp("addOrUpdUserAddressForm").getForm().load({
			deferredRender : false,
			url : __ctxPath + "/addressManager/selectUserAddressById",
			params : {
				id : id
			},
			waitTitle : "请稍候",
			waitMsg : "正在载入数据...",
			root : "data",
			success : function(response, options) {
				var data = options.result.data;
			},
			failure : function(f, g) {
				Ext.MessageBox.show({
							title : "操作信息",
							msg : "数据加载失败!",
							buttons : Ext.MessageBox.OK,
							icon : Ext.MessageBox.ERROR
						});
			}
		});
	}

}